﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using LuxuryResort.WebSite.LRMPDataService;

namespace LuxuryResort.WebSite.NationalAdmin
{
    public partial class SumaryReport : Page
    {
        private readonly RoomServiceClient _svc = new RoomServiceClient();

        private Guid UserID
        {
            get
            {
                if (ViewState["UserID"] == null && HttpContext.Current.User != null)
                {
                    string userName = HttpContext.Current.User.Identity.Name;
                    MembershipUser membershipUser = Membership.GetUser(userName);
                    if (membershipUser != null)
                    {
                        object userId = membershipUser.ProviderUserKey;
                        ViewState["UserID"] = userId;
                    }
                }
                return (Guid) ViewState["UserID"];
            }
        }

        private Guid ResortId
        {
            get { return new Guid(); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LRMPNationalReport[] report = _svc.GetNationalReport(UserID);
            IEnumerable<string> resortList = report.Select(r => r.ResortName).Distinct();

            List<NationalReportEntity> nationtionalReport = (from resortName in resortList
                let localReports =
                    report.Where(r => r.ResortName.Equals(resortName, StringComparison.InvariantCultureIgnoreCase))
                        .Select(r => r.LocalReport)
                select new NationalReportEntity
                {
                    ResortName = resortName,
                    RoomReports = localReports.Where(r => r.Category == "Room").ToList(),
                    FoodReports = localReports.Where(r => r.Category != "Room").ToList(),
                }).ToList();

            rptResort.DataSource = nationtionalReport;
            rptResort.DataBind();
        }
    }

    public class NationalReportEntity
    {
        public string ResortName { get; set; }
        public List<LRMPReport> RoomReports { get; set; }
        public List<LRMPReport> FoodReports { get; set; }
    }
}